 package com.xbongbong.paas.help.service.impl;

 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.xbongbong.paas.constant.PaasConstant;
 import com.xbongbong.paas.enums.FieldTypeEnum;
 import com.xbongbong.paas.field.FieldAttrEntity;
 import com.xbongbong.paas.help.service.FormDataSubFormLinkDataService;
 import com.xbongbong.paas.pojo.dto.FormDataGetDTO;
 import com.xbongbong.paas.toolbox.exception.XbbException;
 import com.xbongbong.parent.parse.data.FormDataLinkDataGetServiceImpl;
 import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Component;

 import javax.annotation.Resource;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;

/**
 * @author: wufeng
 * @date: 2018/11/9 15:40
 * @desrcption: 表单处理关联查询的问题
 */
@Component
public class FormDataSubFormLinkDataGetServiceImpl implements FormDataSubFormLinkDataService<FormDataGetDTO> {

    private static  final Logger LOG = LoggerFactory.getLogger(FormDataSubFormLinkDataGetServiceImpl.class);

    @Resource
    private FormDataLinkDataGetServiceImpl formDataLinkDataGetServiceImpl;

    /**
     * 关联数据的数据查询
     * @param formDataGetDTO
     * @param subFormList 子表单容器
     * @param data 数据详情
     * @param explainMap 字段容器
     * @return
     * @throws XbbException
     */
    @Override
    public Map<String, JSONArray> subFormLinkDataGet(FormDataGetDTO formDataGetDTO, List<String> subFormList, JSONObject data, Map<String, FieldAttrEntity> explainMap) throws XbbException {
        Map<String, JSONArray> subLinkData = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        try {
            // 关联数据标识
            for (String item : subFormList) {
                boolean linkData = false;
                List<String> linkDataList = new ArrayList<>();
                if (explainMap.containsKey(item)) {
                    FieldAttrEntity fieldAttrEntity = explainMap.get(item);
                    Map<String, FieldAttrEntity> subExplainMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                    List<? extends FieldAttrEntity> fieldList = fieldAttrEntity.getSubForm().getItems();
                    // 检索出子表单中关联
                    for (FieldAttrEntity subField : fieldList) {
                        subExplainMap.put(subField.getAttr(), subField);
                        if (subField.getFieldType().equals(FieldTypeEnum.LINK_DATA.getType())) {
                            linkData = true;
                            linkDataList.add(subField.getAttr());
                        }
                    }
                    if (linkData) {
                        JSONArray jsonArray = data.getJSONArray(item);
                        JSONArray resultJsonArray = new JSONArray();
                        if (jsonArray!= null) {
                            for (Object object : jsonArray) {
                                JSONObject jsonObject = (JSONObject) object;
//                                Map<String, Object> resultDataMap = formDataLinkDataGetServiceImpl.formDataLinkDataGet(formDataGetDTO, linkDataList, jsonObject, subExplainMap);
//                                if(resultDataMap != null) {
//                                    Iterator iterator = resultDataMap.keySet().iterator();
//                                    while (iterator.hasNext()) {
//                                        String key = iterator.next().toString();
//                                        resultJsonArray.add(resultDataMap.get(key));
//                                    }
//                                }
                            }
                        }
                        subLinkData.put(item, resultJsonArray);
                    }
                }
            }
        } /*catch (XbbException e) {
            throw e;
        } */ catch (Exception e) {
            LOG.error("获取子表单关联数据失败，", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return subLinkData;
    }
}
